<script lang="ts" setup>
import type { DefaultTheme } from 'vitepress/theme'
import { computed, inject } from 'vue'
import { useData } from 'vitepress'
import { isActive } from '../support/utils.js'
import VPLink from './VPLink.vue'

withDefaults(defineProps<{ item: DefaultTheme.SidebarItem; depth?: number }>(), { depth: 1 })

const { page, frontmatter } = useData()
const maxDepth = computed<number>(() => frontmatter.value.sidebarDepth || Infinity)
const closeSideBar = inject('close-sidebar') as () => void
</script>

<template>
  <VPLink
    class="link"
    :class="{ active: isActive(page.relativePath, item.link), offset: depth > 1 }"
    :href="item.link"
    @click="closeSideBar"
  >
    <span class="link-text" :class="{ light: depth > 1 }">{{ item.text }}</span>
    <template
      v-if="'items' in item && depth < maxDepth"
      v-for="child in item.items"
      :key="child.link"
    >
      <VPSidebarLink :item="child" :depth="depth + 1" />
    </template>
  </VPLink>
</template>

<style scoped>
.link {
  display: block;
  padding: 8px 4px 8px 0;
  margin-bottom:4px;
  color: var(--character-title-text);
  /* color: var(--vp-c-text-2); */
  transition: color 0.5s;
  position: relative;
}

.link.offset {
  padding-left: 16px;
}

.link:hover {
  color: #2A87FF;
}

.link.active {
  color: #2A87FF;
}
.link:hover::after,
.link.active::after{
    content: '';
    background: var(--nav-link-bg);
    position: absolute;
    left: -16px;
    top: 0;
    bottom: 0;
    right: 0;
    border-radius: 6px;
    z-index: -1;
}

.link :deep(.icon) {
  width: 12px;
  height: 12px;
  fill: currentColor;
}

.link-text {
  line-height: 20px;
  font-size: 14px;
  font-weight: 500;
}

.link-text.light {
  font-size: 13px;
  font-weight: 400;
}
</style>
